using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace KisloidTools.LogReader
{
    public partial class MainForm : Form
    {
        private string _logFileName;

        public MainForm(string logFileName)
        {
            InitializeComponent();

            if (logFileName != null)
            {
                LoadLogFile(logFileName);
            }
        }

        private void loadLogButton_Click(object sender, EventArgs e)
        {
            _logFileName = GetLogFileName();

            LoadLogFile(_logFileName);
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddDataGridViewRow(string date, string time, string message)
        {
            string[] row = new string[] {date, time, message};

            dataGridView.Rows.Add(row);
        }

        private string GetLogFileName()
        {
            string result = "";

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "log files (*.xml)|*.xml";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                result = openFileDialog.FileName;
            }

            return result;
        }

        private void LoadLogFile(string logFileName)
        {
            XmlReaderSettings xmlSettings = new XmlReaderSettings();
            xmlSettings.CheckCharacters = true;

            try
            {
                using (XmlReader xmlReader = XmlReader.Create(logFileName, xmlSettings))
                {
                    string date = "";
                    string time = "";
                    string message = "";

                    bool logMessageReading = false;

                    while (xmlReader.Read())
                    {
                        switch (xmlReader.NodeType)
                        {
                            case XmlNodeType.Element:
                                if (xmlReader.Name == "LogMessage")
                                {
                                    logMessageReading = true;

                                    // read attributes
                                    xmlReader.MoveToAttribute("Date");
                                    date = xmlReader.Value;
                                    xmlReader.MoveToAttribute("Time");
                                    time = xmlReader.Value;
                                }

                                break;
                            case XmlNodeType.EndElement:
                                if (xmlReader.Name == "LogMessage")
                                {
                                    logMessageReading = false;

                                    AddDataGridViewRow(date, time, message);
                                }

                                break;
                            case XmlNodeType.Text:
                                if (logMessageReading)
                                {
                                    message = xmlReader.Value;
                                }

                                break;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error message",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
